import Pages from '../../pages/pages.vue'
import itemNew from '../item-new/item-new.vue'
import { setMenuType, getItemList, deleteItem, showMsg, swapItem } from '../../../vuex/action'

export default {
  name: 'item-list',
  components: {
    'vue-pages': Pages,
    'item-new': itemNew
  },
  data() {
    return {
      showItemNew: false,
      title: '',
      it: {},
      cList: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.inviteListFresh,
    },
    actions: {
      xSetMenuType: setMenuType,
      xGetItemList: getItemList,
      xDeleteItem: deleteItem,
      xShowMsg: showMsg,
      xSwap: swapItem,
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetItemList({
        start: start,
        limit: limit,
        classifyId: this.$route.params.id
      })
        .then((data) => {
          this.cList = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    newItem(it, title) {
      this.showItemNew = true
      this.it = it
      this.title = title
    },
    deleteItem(itemId){
      this.xDeleteItem({
        itemId: itemId
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    swap(id1, id2) {
      this.xSwap({
        fromId: id1,
        toId: id2
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
